package us.wili.dev.client.service.impl;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import us.wili.dev.client.dao.ClientGoodsMapper;
import us.wili.dev.client.model.Detail;
import us.wili.dev.client.model.req.FabricReq;
import us.wili.dev.client.model.req.SearchReq;
import us.wili.dev.client.model.rsp.GoodsRsp;
import us.wili.dev.client.model.rsp.ShowClothRsp;
import us.wili.dev.client.service.ClientGoodsService;
import us.wili.dev.common.model.Pager;
import us.wili.dev.model.entity.*;
import us.wili.dev.model.example.GoodsExample;
import us.wili.dev.service.impl.BaseGoodsServiceImpl;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class ClientGoodsServiceImpl extends BaseGoodsServiceImpl implements ClientGoodsService {
    @Resource
    private ClientGoodsMapper clientGoodsMapper;

    @Override
    public GoodsRsp goodsdeta(Long id) {
        Goods goods = seargood(id);
        List<Goods>hot = hotgoods();
        List<Goods>list = new ArrayList<>();
        if (hot!=null){
            for (Goods goods1 : hot) {
                Goods x = new Goods();
                x.setId(goods1.getId());
                x.setGoodsName(goods1.getGoodsName());
                x.setGoodsPic(goods1.getGoodsPic());
                x.setGoodsPrice(goods1.getGoodsPrice());
                list.add(x);
            }
        }
        return GoodsRsp.Initialization(goods,list);
    }

    @Override
    public Goods rotaGood(Rota rota) {
        GoodsExample example = new GoodsExample();
        example.createCriteria().andIdEqualTo(rota.getDocid());
        return clientGoodsMapper.selectOneByExample(example);
    }

    @Override
    public GoodsRsp hot(Hot hot) {
        Goods goods = searHotGoods(hot);
        return GoodsRsp.hot(goods);
    }

    @Override
    public Pager<GoodsRsp> search(SearchReq req) {
        Pager<GoodsRsp> pager = new Pager<>();
        List<GoodsRsp> list= new ArrayList<>();
        List<Goods> goodsList = search(req,pager);
        for (Goods goods : goodsList) {
            GoodsRsp rsp = GoodsRsp.search(goods);
            list.add(rsp);
        }
        pager.setArray(list);
        return pager;
    }

    @Override
    public Goods shopcartgoods(Shopcart shopcart) {
        GoodsExample example = new GoodsExample();
        example.createCriteria().andIdEqualTo(shopcart.getGoodsId());
        return clientGoodsMapper.selectOneByExample(example);
    }

    @Override
    public Pager<GoodsRsp> classfabric(FabricReq req) {
        Pager<GoodsRsp> pager = new Pager<>();
        List<GoodsRsp> list= new ArrayList<>();
        List<Goods> goodsList = searchFabric(req,pager);
        for (Goods goods : goodsList) {
            GoodsRsp rsp = GoodsRsp.classfabric(goods);
            list.add(rsp);
        }
        pager.setArray(list);
        return pager;
    }

    @Override
    public Goods ordergoods(Order order) {
        GoodsExample example = new GoodsExample();
        example.createCriteria();//----------------------
        return clientGoodsMapper.selectOneByExample(example);
    }



    @Override
    public Goods collectiongoods(Collection collection) {
        GoodsExample example = new GoodsExample();
        example.createCriteria().andIdEqualTo(collection.getGoodsId());
        return clientGoodsMapper.selectOneByExample(example);
    }

    @Override
    public Goods orderGoods(Detail detail1) {
        GoodsExample example = new GoodsExample();
        example.createCriteria().andIdEqualTo(detail1.getGoods_id());
        return clientGoodsMapper.selectOneByExample(example);
    }

    /**
     * 面料展示
     * @return
     */
    @Override
    public List<ShowClothRsp> ShowCloth() {
        List<ShowClothRsp> list = new ArrayList<>();
        List<Goods> goodsList = showcloth();
        for (Goods goods : goodsList) {
            ShowClothRsp showClothRsp = ShowClothRsp.toShowCloth(goods);
            list.add(showClothRsp);
        }
        return list;
    }

    /**
     * 活动推荐
     * @param id
     * @return
     */
    @Override
    public List<ShowClothRsp> ActivityDeta(Long id) {
        List<ShowClothRsp> list = new ArrayList<>();
        List<Goods> goodsList = SearActivityDeta(id);
        for (Goods goods : goodsList) {
            ShowClothRsp showClothRsp = ShowClothRsp.toShowCloth(goods);
            list.add(showClothRsp);
        }
        return list;
    }

    public List<Goods> SearActivityDeta(Long id){
        GoodsExample example = new GoodsExample();
        example.createCriteria().andGoodsActivityEqualTo(id.intValue());
        return clientGoodsMapper.selectByExample(example);
    }

    public List<Goods> showcloth(){
        GoodsExample example = new GoodsExample();
        example.createCriteria();
        example.orderBy().descCreatedAt();
        example.limit(0,6);
        return clientGoodsMapper.selectByExample(example);
    }

    public Goods seargood(Long id){
        GoodsExample example = new GoodsExample();
        example.createCriteria().andIdEqualTo(id);
        return clientGoodsMapper.selectOneByExample(example);
    }

    public List<Goods> hotgoods(){
        GoodsExample example = new GoodsExample();
        example.limit(0,4);
        example.orderBy().descCreatedAt();
        return clientGoodsMapper.selectByExample(example);
    }

    public Goods searHotGoods(Hot hot){
        GoodsExample example = new GoodsExample();
        example.createCriteria().andIdEqualTo(hot.getDosid());
        return clientGoodsMapper.selectOneByExample(example);
    }

    public List<Goods> search(SearchReq req, Pager<GoodsRsp> pager){
        GoodsExample example = new GoodsExample();
        if (!StringUtils.isEmpty(req.getKeyword())){
            example.createCriteria().andGoodsNameLike("%"+req.getKeyword()+"%");
        }
        example.createCriteria();
        example.orderBy().descCreatedAt();
        example.limit(Math.toIntExact(req.getOffset()), Math.toIntExact(req.getCount()));
        Long totel = clientGoodsMapper.countByExample(example);
        pager.setTotal(totel);
        return clientGoodsMapper.selectByExample(example);
    }

    public List<Goods> searchFabric(FabricReq req, Pager<GoodsRsp> pager){
        GoodsExample example = new GoodsExample();
        if (req!=null){
            example.createCriteria().andGoodsMaterialEqualTo(req.getType().intValue());
        }
        else {
            example.createCriteria();
        }
        example.orderBy().descCreatedAt();
        example.limit(Math.toIntExact(req.getOffset()), Math.toIntExact(req.getCount()));
        Long totel = clientGoodsMapper.countByExample(example);
        pager.setTotal(totel);
        return clientGoodsMapper.selectByExample(example);
    }
}
